<template>
	<div>
		<el-row :gutter="20" type="flex">
			<el-col :span="6">
				<el-input placeholder="请输入文章标题" v-model="listQueryIn.title" clearable size="small"></el-input>
			</el-col>
			<el-col :span="12">
				<btn-search @click.native="refreshList()"></btn-search>
				<el-button type="primary" size="small" icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
				<btn-add name="新增文章" :disabled="curColumn.isOutsite != 2" @click.native="handleAdd()"></btn-add>
				<btn-delete :disabled="multipleSelection.length == 0" @click.native="handleBatchDelete()"></btn-delete>
				<el-button type="primary" size="small" @click="handleBatchPublish" plain :disabled="multipleSelection.length == 0">批量发布</el-button>
			</el-col>
		</el-row>
	</div>
</template>
<script>
import { throttle } from 'lodash';
export default {
	data() {
		return {
			multipleSelection: [],
			listQueryIn: {
				title: ''
			}
		};
	},
	props: ['curColumn'],
	watch: {
		curColumn: {
			handler(data) {
				this.multipleSelection = [];
			},
			immediate: true
		}
	},
	mounted() {
		this.$bus.$on('deleteBatchNewsContents', (v) => {
			this.multipleSelection = v;
		});
	},
	beforeDestroy() {
		this.$bus.$off('deleteBatchNewsContents');
	},
	methods: {
		handleInput: throttle(function () {
			this.refreshList();
		}, 2000),
		refreshList() {
			this.$bus.$emit('refreshNewsContentsList', this.listQueryIn,1);
		},
		handleAdd() {
			this.$bus.$emit('showDialogNewsContentsAdd', { columnNames: this.curColumn.name });
		},
		//重置
		handleReset() {
			this.listQueryIn.title = '';
			this.$store.commit('SET_COLUMN', {});
			this.$bus.$emit('refreshNewsContentsList', this.listQueryIn);
		},
		//批量删除
		handleBatchDelete() {
			if (!this.multipleSelection.length) {
				this.warnMsg('请选择要删除的数据');
				return;
			}
			this.deleteTips(this.deleteAll);
		},
		//批量删除
		async deleteAll() {
			let ids = this.multipleSelection.map((v) => v.id);
			await this.$store.dispatch('deleteBatchNewsContents', ids);
			this.$bus.$emit('refreshNewsContentsList');
			this.multipleSelection = [];
		},
		//批量发布
		async handleBatchPublish() {
			if (!this.multipleSelection.length) {
				this.warnMsg('请选择要发布的文章');
				return;
			}
			let passList = this.multipleSelection.filter((v) => v.auditStatus == 2);
			if (!passList.length) {
				this.warnMsg('请选择已审核通过的文章');
				return;
			}
			let obj = {
				ids: passList.map((v) => v.id),
				status: 1
			};
			await this.$store.dispatch('changeStatusBatchNewsContents', obj);
			this.$bus.$emit('refreshNewsContentsList');
			this.multipleSelection = [];
		}
	}
};
</script>
